/*
 * @Description: 
 * @Author: changqing
 * @Date: 2021-08-29 20:06:39
 * @LastEditTime: 2021-08-29 20:08:25
 * @LastEditors: changqing
 * @Usage: 
 */

// 可以根据需要自定义逻辑决定事件发生，只要我们装将逻辑写好就好，有两个参数

// addHandler：当我们订阅时subscribe时，调用此方法决定如何处理事件逻辑
// removeHandler：当我们取消订阅时unsubscribe时，调用些方法将原来的事件逻辑取消


var { fromEventPattern } = require('rxjs');

const addClickHandler = (handler) => {
  console.log("fromEventPattern 示例: 自定义注册click事件");
  document.addEventListener("click", (event) => handler(event));
};

const removeClickHandler = (handler) => {
  console.log("fromEventPattern 示例: 取消自定义注册click事件");
  document.removeEventListener("click", handler);
};

const source = fromEventPattern(addClickHandler, removeClickHandler);

const subscription = source.subscribe((event) =>
  console.log("fromEventPattern 示例: 触发document click 事件", event)
);

setTimeout(() => {
	subscription.unsubscribe();
}, 3000);